package com.hu.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.hu.common.annotation.Log;
import com.hu.common.core.controller.BaseController;
import com.hu.common.core.domain.AjaxResult;
import com.hu.common.enums.BusinessType;
import com.hu.system.domain.ProfitAftersale;
import com.hu.system.service.IProfitAftersaleService;
import com.hu.common.utils.poi.ExcelUtil;
import com.hu.common.core.page.TableDataInfo;

/**
 * 售后明细Controller
 *
 * @author hu
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/business/profit/aftersale")
public class ProfitAftersaleController extends BaseController {
    @Autowired
    private IProfitAftersaleService profitAftersaleService;

    /**
     * 查询售后明细列表
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProfitAftersale profitAftersale) {
        startPage();
        List<ProfitAftersale> list = profitAftersaleService.selectProfitAftersaleList(profitAftersale);
        return getDataTable(list);
    }

    /**
     * 获取售后明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:query')")
    @GetMapping(value = "/{aftersaleId}")
    public AjaxResult getInfo(@PathVariable("aftersaleId") Long aftersaleId) {
        ProfitAftersale profitAftersale = profitAftersaleService.selectProfitAftersaleByAftersaleId(aftersaleId);
        if (profitAftersale == null) {
            return error("您没有权限查看该售后记录或记录不存在");
        }
        return success(profitAftersale);
    }

    /**
     * 新增售后明细
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:add')")
    @Log(title = "售后明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProfitAftersale profitAftersale) {
        return toAjax(profitAftersaleService.insertProfitAftersale(profitAftersale));
    }

    /**
     * 修改售后明细
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:edit')")
    @Log(title = "售后明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProfitAftersale profitAftersale) {
        return toAjax(profitAftersaleService.updateProfitAftersale(profitAftersale));
    }

    /**
     * 删除售后明细
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:remove')")
    @Log(title = "售后明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{aftersaleIds}")
    public AjaxResult remove(@PathVariable Long[] aftersaleIds) {
        return toAjax(profitAftersaleService.deleteProfitAftersaleByAftersaleIds(aftersaleIds));
    }

    /**
     * 导入售后明细（自动识别并读取两个sheet）
     */
    @PreAuthorize("@ss.hasPermi('profit:aftersale:import')")
    @Log(title = "售后明细", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long shopId) throws Exception {
        // 校验店铺ID
        if (shopId == null) {
            return error("请选择所属店铺");
        }

        String message = profitAftersaleService.importProfitAftersale(file, updateSupport, shopId);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ProfitAftersale> util = new ExcelUtil<ProfitAftersale>(ProfitAftersale.class);
        util.importTemplateExcel(response, "售后明细数据");
    }
}

